# -*- coding: utf-8 -*-

from Common.mysql import db
from Common.public_method import utc_8
from src.ApiTest.ApiCase.Database.api_case_database import ApiCase
from sqlalchemy.exc import SQLAlchemyError
from Common.yaml_method import YamlMethod


class UpdateApiCase:
    """
    更新接口测试用例接口
    """

    @staticmethod
    def update_api_case(case_id, case_name, project_id, project_name, request_info, update_user):
        """
        更新接口用例信息接口
        :param case_id: 接口用例ID
        :param case_name: 接口测试测试用例名称
        :param project_id: 项目ID
        :param project_name: 项目名称
        :param request_info: 接口请求信息
        :param update_user: 更新人
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']
        update_time = utc_8()

        info = ApiCase.query.filter_by(id=case_id).first()

        if info:
            info.caseName = case_name
            info.projectId = project_id
            info.projectName = project_name
            info.requestInfo = request_info
            info.update_user = update_user
            info.update_time = update_time
            try:
                db.session.commit()
            except SQLAlchemyError as e:
                db.session.rollback()
                res = {
                    'code': code[6],
                    'data': [{
                        'error': e
                    }],
                    'message': '接口测试用例信息更新失败'
                }
                return res
            res = {
                'code': code[0],
                'data': [],
                'message': '接口测试用例信息更新成功'
            }
            return res
        else:
            res = {
                'code': code[3],
                'data': [],
                'message': '接口测试用例信息不存在'
            }
            return res


